//
//  383. 赎金信.swift
//  LeetCodeTrain
//
//  Created by rjb on 2021/7/14.
//  Copyright © 2021 rjb. All rights reserved.
//

import Foundation

class Solution383 {
    // 采用数组当作hash。实际上效率是更高的
    func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
        var array = Array(repeating: 0, count: 26)
        for item in Array(magazine) {
            let index = Int(item.asciiValue! - Character("a").asciiValue!)
            array[index] += 1
        }
        for item in Array(ransomNote) {
            let index = Int(item.asciiValue! - Character("a").asciiValue!)
            if array[index] <= 0 {
                return false
            }
            array[index] -=  1
        }
        return true
    }
    
    static func test() {
        let solution = Solution383()
        let ransomNote = "aa", magazine = "ab"
        let result = solution.canConstruct(ransomNote, magazine)
        print(result)
    }
}
